package com.vanguard.commons.utils;

import com.vanguard.webservice.WebServiceManage;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * @Title: WebService工具类
 * @Description: 根据WebServiceUrl获取WebServiceManage对象
 * @Author: vanguard
 * @Version: 1.0
 * @Date: 2019/2/13
 */
@Component
public class WebServiceUtils {

    public static WebServiceManage webServiceManage;
    private static Logger logger = LoggerFactory.getLogger(WebServiceUtils.class);

    /**
     * 获取WebServiceManage单例
     *
     * @return
     */
    public static WebServiceManage getManage(String webServiceUrl) {
        if (null == webServiceManage) {
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
            // 添加CXF默认的请求信息日志拦截器
            factory.getInInterceptors().add(new LoggingInInterceptor());
            // 添加CXF默认的响应信息日志拦截器
            factory.getOutInterceptors().add(new LoggingOutInterceptor());
            factory.setServiceClass(WebServiceManage.class);
            // 设置WebService的WSDL地址(从sysConfig.xml获取WEBSERVICE_ADDRESS项)
            factory.setAddress(webServiceUrl);
            webServiceManage = (WebServiceManage) factory.create();
            Client proxy = ClientProxy.getClient(webServiceManage);
            HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
            HTTPClientPolicy policy = new HTTPClientPolicy();
            policy.setAllowChunking(false);
            // 连接服务器超时时间 30秒
            policy.setConnectionTimeout(30 * 1000);
            // 等待服务器响应超时时间 1分钟
            policy.setReceiveTimeout(60 * 1000);
            conduit.setClient(policy);
        }
        return webServiceManage;
    }
}
